home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / RAVE SDK 1.06 GM for MacOS / Example Projects / RaveTest / RAVE_Window.c < prev   
Encoding:
C/C++ Source or Header  |  1996-03-19  |  12.9 KB  |  561 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        RAVE_Window.c                                             **
  4.  **                                                                          **
  5.  **     Purpose:     Window/GDevice stuff for RAVE test app                     **
  6.  **                                                                          **
  7.  **     Author:        Mike W. Kelley                                             **
  8.  **                                                                             **
  9.  **                    2/3/95    Revised for 0.9 SDK release                         **
  10.  **                    2/23/96 BHP - Revised for 1.05 relase                     **
  11.  **                                                                          **
  12.  **     Copyright (C) 1994-95 Apple Computer, Inc.  All rights reserved.     **
  13.  **                                                                          **
  14.  *****************************************************************************/
  15.  
  16.  
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21.  
  22. /* Macintosh */
  23. #include <Types.h>
  24. #include <Resources.h>
  25. #include <QuickDraw.h>
  26. #include <Fonts.h>
  27. #include <Events.h>
  28. #include <Windows.h>
  29. #include <Menus.h>
  30. #include <TextEdit.h>
  31. #include <Dialogs.h>
  32. #include <Desk.h>
  33. #include <ToolUtils.h>
  34. #include <Memory.h>
  35. #include <SegLoad.h>
  36. #include <Files.h>
  37. #include <OSUtils.h>
  38. #include <OSEvents.h>
  39. #include <DiskInit.h>
  40. #include <Packages.h>
  41.  
  42. /* Project */
  43. #include "RAVE.h"
  44. #include "RAVE_Test.h"
  45.  
  46. #include "QD3DAcceleration.h"
  47.  
  48. /*******************************************************************************************
  49.  *
  50.  * Static variables used by this file's functions only.
  51.  *
  52.  ******************************************************************************************/
  53.  
  54. static    GDHandle        gGDevice;
  55. static    WindowPtr        gRAVE_Window [kRAVE_Windows];
  56. static    RgnHandle        gClipRgn [kRAVE_Windows];
  57.  
  58. /*******************************************************************************************
  59.  *
  60.  * Utility functions
  61.  *
  62.  ******************************************************************************************/
  63.  
  64. void InitToolbox (void)
  65. {
  66.     InitGraf((Ptr) &qd.thePort);
  67.     InitFonts();
  68.     InitWindows();
  69.     InitMenus();
  70.     FlushEvents(everyEvent,0);
  71.     TEInit();
  72.     InitDialogs(0L);
  73.     InitCursor();
  74. }
  75.  
  76. /************************************************************************************************
  77.  *
  78.  * Error handling stuff.
  79.  *
  80.  ***********************************************************************************************/
  81.  
  82. void RAVE_Error (
  83.     const char        *message)
  84. {
  85.     char            ErrorStr[256];
  86.  
  87.     sprintf (&ErrorStr[1], "RAVE_Error: %s\n", message);
  88.     ErrorStr[0] = strlen (&ErrorStr[1]) - 1;
  89.     DebugStr ( (ConstStr255Param) ErrorStr);
  90. }
  91.  
  92. /************************************************************************************************
  93.  *
  94.  * Choose the software drawing engine for 'windowNumber'. This returns the engine name.
  95.  *
  96.  ***********************************************************************************************/
  97.  
  98. static TQAEngine *ChooseSoftwareEngine (
  99.     TQADevice        *device,
  100.     long            windowNumber,
  101.     char            engineName[64])        /* Out */
  102. {
  103.     TQAEngine        *engine;
  104.     long             vendorID;
  105.     long             engineID;
  106.     TQAError        gestaltStatus;
  107.     long            engineNameLength;
  108.     
  109.     for (engine = QADeviceGetFirstEngine (device);
  110.             engine;
  111.             engine = QADeviceGetNextEngine (device, engine))
  112.     {
  113.         /*
  114.          * Search to see if this engine is already being used.
  115.          */
  116.  
  117.         gestaltStatus = QAEngineGestalt (engine, kQAGestalt_VendorID, &vendorID);
  118.         gestaltStatus = QAEngineGestalt (engine, kQAGestalt_EngineID, &engineID);
  119.         
  120.         if(vendorID == kQAVendor_Apple && engineID == kQAEngine_AppleSW)
  121.         {
  122.             break;
  123.         }
  124.     }
  125.     
  126.     if(engine)
  127.     {
  128.         gestaltStatus = QAEngineGestalt (engine, kQAGestalt_ASCIINameLength, &engineNameLength);
  129.         if ((gestaltStatus == kQANoErr) && (engineNameLength < 63))
  130.         {
  131.             gestaltStatus = QAEngineGestalt (engine, kQAGestalt_ASCIIName, &engineName[1]);
  132.         }
  133.         if (gestaltStatus != kQANoErr)
  134.         {
  135.             /*
  136.              * Gestalt call failed!
  137.              */
  138.             
  139.             sprintf (&engineName[1], "<Gestalt Error>");
  140.             engineName[0] = strlen (&engineName[1]);
  141.         }
  142.         else
  143.         {
  144.             engineName[0] = engineNameLength;
  145.         }
  146.     }
  147.     else
  148.     {
  149.         sprintf (&engineName[1], "<No Software Engine>");
  150.         engineName[0] = strlen (&engineName[1]);
  151.     }
  152.     
  153.  
  154.     return engine;
  155. }
  156.  
  157. /************************************************************************************************
  158.  *
  159.  * Choose a drawing engine for 'windowNumber'. This returns the engine name.
  160.  *
  161.  ***********************************************************************************************/
  162.  
  163. static TQAEngine *ChooseEngine (
  164.     TQADevice        *device,
  165.     long            windowNumber,
  166.     char            engineName[64])        /* Out */
  167. {
  168.     TQAEngine        *engine;
  169.     long            i;
  170.     Boolean            alreadyUsed;
  171.     TQAError        gestaltStatus;
  172.     long            engineNameLength;
  173.     long             vendorID;
  174.     long             engineID;
  175.     long             revision;
  176.     
  177.     for (engine = QADeviceGetFirstEngine (device);
  178.             engine;
  179.             engine = QADeviceGetNextEngine (device, engine))
  180.     {
  181.         /*
  182.          * Search to see if this engine is already being used.
  183.          */
  184.         
  185.         for (i = 0, alreadyUsed = false; i < windowNumber; ++i)
  186.         {
  187.             if (gEngine [i] == engine)
  188.             {
  189.                 alreadyUsed = true;
  190.             }
  191.         }
  192.         
  193.         gestaltStatus = QAEngineGestalt (engine, kQAGestalt_VendorID, &vendorID);
  194.         gestaltStatus = QAEngineGestalt (engine, kQAGestalt_EngineID, &engineID);
  195.         
  196.         if(vendorID == kQAVendor_Apple && engineID == kQAEngine_AppleHW)
  197.         {
  198.             gestaltStatus = QAEngineGestalt (engine, kQAGestalt_Revision, &revision);
  199.             
  200.             if(revision < 1)
  201.             {
  202.                 /* Make sure that we don't use revision 0 of the Apple QD3D HW Accelerator */
  203.                 continue;
  204.             }
  205.         }
  206.         
  207.         if ( ! alreadyUsed)
  208.         {
  209.             /*
  210.              * Use this engine.
  211.              */
  212.             
  213.             break;
  214.         }
  215.     }
  216.     
  217.     if (engine)
  218.     {
  219.         /*
  220.          * A unique drawing engine was found. Do a gestalt call to get the
  221.          * engine name.
  222.          */
  223.         
  224.         gestaltStatus = QAEngineGestalt (engine, kQAGestalt_ASCIINameLength, &engineNameLength);
  225.         if ((gestaltStatus == kQANoErr) && (engineNameLength < 63))
  226.         {
  227.             gestaltStatus = QAEngineGestalt (engine, kQAGestalt_ASCIIName, &engineName[1]);
  228.         }
  229.         
  230.         if (gestaltStatus != kQANoErr)
  231.         {
  232.             /*
  233.              * Gestalt call failed!
  234.              */
  235.             
  236.             sprintf (&engineName[1], "<Gestalt Error>");
  237.             engineName[0] = strlen (&engineName[1]);
  238.         }
  239.         else
  240.         {
  241.             engineName[0] = engineNameLength;
  242.         }
  243.     }
  244.     else
  245.     {
  246.         /*
  247.          * No unused engine was found. Assign a NULL engine pointer, and
  248.          * set a dummy engine name.
  249.          */
  250.         
  251.         sprintf (&engineName[1], "<No Drawing Engine>");
  252.         engineName[0] = strlen (&engineName[1]);
  253.     }
  254.     
  255.     return (engine);
  256. }
  257.  
  258. /*******************************************************************************************
  259.  *
  260.  * Create test windows on the deepest GDevice.
  261.  *
  262.  ******************************************************************************************/
  263.  
  264. Boolean CreateTestWindows (
  265.     long            maxWidth,
  266.     long            maxHeight)
  267. {
  268.     GDHandle        colourGDevice, deepestGDevice;
  269.     long            deviceDepth, maxDepth;
  270.     Rect            deviceRect;
  271.     Rect            windowRect;
  272.     long            deviceWidth, deviceHeight;
  273.     long            windowWidth, windowHeight;
  274.     TQADevice        device;
  275.     char            engineName[64];
  276.  
  277.     /*
  278.      * Find the deepest color display.
  279.      */
  280.     
  281.     maxDepth = 0;
  282.     
  283.     for (colourGDevice = GetDeviceList(), deepestGDevice = NULL;
  284.             colourGDevice;
  285.             colourGDevice = GetNextDevice (colourGDevice))
  286.     {
  287.         deviceDepth = (*(*colourGDevice)->gdPMap)->pixelSize;
  288.         
  289.         if (deviceDepth > maxDepth)
  290.         {
  291.             maxDepth = deviceDepth;
  292.             deepestGDevice = colourGDevice;
  293.         }
  294.     }
  295.     gGDevice = deepestGDevice;
  296.     
  297.     /*
  298.      * Open two side-by-side windows which almost fill the device, or to
  299.      * maxWidth, maxHeight.
  300.      */
  301.  
  302.     deviceRect = (*deepestGDevice)->gdRect;
  303.     
  304.     deviceWidth = (deviceRect.right - deviceRect.left) - 2 * kDeviceWindowBorder;
  305.     deviceHeight = (deviceRect.bottom - deviceRect.top) - 2 * kDeviceWindowBorder;
  306.     
  307.     windowWidth = (deviceWidth / 2) - 2 * kDeviceWindowBorder;
  308.     windowHeight = deviceHeight - 2 * kDeviceWindowBorder;
  309.     
  310.     if (windowWidth > maxWidth)
  311.     {
  312.         windowWidth = maxWidth;
  313.     }
  314.     if (windowHeight > maxHeight)
  315.     {
  316.         windowHeight = maxHeight;
  317.     }
  318.     
  319.     gWindowWidth = windowWidth;
  320.     gWindowHeight = windowHeight;
  321.     
  322.     device.deviceType = kQADeviceGDevice;
  323.     device.device.gDevice = deepestGDevice;
  324.  
  325.     /*
  326.      * Choose a drawing engine for the left window, and create the window.
  327.      */
  328.     
  329.     gEngine[0] = ChooseSoftwareEngine (&device, 0, engineName);
  330.     
  331.     windowRect.top = (deviceRect.top + deviceRect.bottom) / 2 - windowHeight / 2;
  332.     windowRect.bottom = windowRect.top + windowHeight;
  333.  
  334.     windowRect.left = (deviceRect.left * 3 + deviceRect.right) / 4 - windowWidth / 2;
  335.     windowRect.right = windowRect.left + windowWidth;
  336.  
  337.     gRAVE_Window[0] = NewCWindow (nil, &windowRect, (ConstStr255Param) engineName, true, zoomDocProc, nil, true, 0);
  338.     
  339.     /*
  340.      * Choose a drawing engine for the right window, and create the window.
  341.      */
  342.     
  343.     gEngine[1] = ChooseEngine (&device, 1, engineName);
  344.     
  345.     windowRect.left = (deviceRect.left + deviceRect.right * 3) / 4 - windowWidth / 2;
  346.     windowRect.right = windowRect.left + windowWidth;
  347.  
  348.     gRAVE_Window[1] = NewCWindow (nil, &windowRect, (ConstStr255Param) engineName, true, zoomDocProc, nil, true, 0);
  349.  
  350.     /*
  351.      * If both window creations were successful, return true.
  352.      */
  353.     
  354.     return (gRAVE_Window[0] && gRAVE_Window[1]);
  355. }
  356.  
  357. /*******************************************************************************************
  358.  *
  359.  * Create test windows on the deepest GDevice.
  360.  *
  361.  ******************************************************************************************/
  362.  
  363. void ClearWindows (void)
  364. {
  365.     long            i;
  366.     WindowPtr        window;
  367.     
  368.     for (i = 0; i < kRAVE_Windows; ++i)
  369.     {
  370.         GrafPtr        previousPort;
  371.         
  372.         window = gRAVE_Window [i];
  373.         
  374.         GetPort (&previousPort);
  375.         SetPort (window);
  376.         EraseRect (&window->portRect);
  377.         SetPort (previousPort);
  378.     }
  379. }
  380.  
  381. /*******************************************************************************************
  382.  *
  383.  * Create test clip region (a round rect) for each test window.
  384.  *
  385.  ******************************************************************************************/
  386.  
  387. Boolean CreateWindowClip (void)
  388. {
  389.     long            i;
  390.     Point             topLeft, botRight;
  391.     WindowPtr        window;
  392.     Rect            windowRect, clipRect;
  393.     
  394.     for (i = 0; i < kRAVE_Windows; ++i)
  395.     {
  396.         GrafPtr        previousPort;
  397.  
  398.         window = gRAVE_Window [i];
  399.         
  400.         /*
  401.          * Get the boundaries of the target window, and compute its size.
  402.          */
  403.         
  404.         topLeft.v = window->portRect.top;
  405.         topLeft.h = window->portRect.left;
  406.         botRight.v = window->portRect.bottom;
  407.         botRight.h = window->portRect.right;
  408.     
  409.         /*
  410.          * Convert window boundaries to global co-ordinates.
  411.          */
  412.         
  413.         GetPort (&previousPort);
  414.         SetPort (window);
  415.         LocalToGlobal (&topLeft);
  416.         LocalToGlobal (&botRight);
  417.         SetPort (previousPort);
  418.     
  419.         /*
  420.          * Create the window bound rectangle in device coordinates.
  421.          */
  422.         
  423.         windowRect.top = topLeft.v - (*gGDevice)->gdRect.top;
  424.         windowRect.left = topLeft.h - (*gGDevice)->gdRect.left;
  425.         windowRect.bottom = botRight.v - (*gGDevice)->gdRect.top;
  426.         windowRect.right = botRight.h - (*gGDevice)->gdRect.left;
  427.         
  428.         /*
  429.          * Build the clip region for this window.
  430.          */
  431.         
  432.         if ( ! (gClipRgn[i] = NewRgn()))
  433.         {
  434.             return (false);
  435.         }
  436.         
  437.         OpenRgn();
  438.         
  439.         clipRect.top = windowRect.top + kWindowClipBoundary;
  440.         clipRect.left = windowRect.left + kWindowClipBoundary;
  441.         clipRect.bottom = windowRect.bottom - kWindowClipBoundary;
  442.         clipRect.right = windowRect.right - kWindowClipBoundary;
  443.         
  444.         FrameRoundRect (&clipRect, kClipRoundRect, kClipRoundRect);
  445.         CloseRgn (gClipRgn[i]);
  446.     }
  447.     
  448.     return (true);
  449. }
  450.  
  451. /*******************************************************************************************
  452.  *
  453.  * Create draw contexts for the test windows.
  454.  *
  455.  ******************************************************************************************/
  456.  
  457. Boolean TestDrawContextNew (
  458.     unsigned long    flags,
  459.     Boolean            do2DClipping)
  460. {
  461.     WindowPtr            window;
  462.     Point                 topLeft, botRight;
  463.     TQARect                windowRect;
  464.     long                i;
  465.     TQADevice            device;
  466.     TQAError            status;
  467.     
  468.     for (i = 0; i < kRAVE_Windows; ++i)
  469.     {
  470.         if (gEngine [i])
  471.         {
  472.             TQAClip        clip;
  473.             
  474.             window = gRAVE_Window [i];
  475.             
  476.             /*
  477.              * Get the boundaries of the target window, and compute its size.
  478.              */
  479.             
  480.             topLeft.v = window->portRect.top;
  481.             topLeft.h = window->portRect.left;
  482.             botRight.v = window->portRect.bottom;
  483.             botRight.h = window->portRect.right;
  484.         
  485.             /*
  486.              * Convert window boundaries to global co-ordinates.
  487.              */
  488.             
  489.             SetPort (window);
  490.             LocalToGlobal (&topLeft);
  491.             LocalToGlobal (&botRight);
  492.         
  493.             /*
  494.              * Create the bound rectangle in device coordinates.
  495.              */
  496.             
  497.             windowRect.top = topLeft.v - (*gGDevice)->gdRect.top;
  498.             windowRect.left = topLeft.h - (*gGDevice)->gdRect.left;
  499.             windowRect.bottom = botRight.v - (*gGDevice)->gdRect.top;
  500.             windowRect.right = botRight.h - (*gGDevice)->gdRect.left;
  501.         
  502.             /*
  503.              * Create the draw context.
  504.              */
  505.             
  506.             device.deviceType = kQADeviceGDevice;
  507.             device.device.gDevice = gGDevice;
  508.     
  509.             clip.clipType = kQAClipRgn;
  510.             clip.clip.clipRgn = gClipRgn[i];
  511.             
  512.             status = QADrawContextNew (&device, &windowRect,
  513.                     do2DClipping ? &clip : NULL, gEngine [i], flags, &gDrawContext [i]);
  514.             
  515.             if (status != kQANoErr)
  516.             {
  517.                 RAVE_Error ("Could not create draw context");
  518.                 return (false);
  519.             }
  520.         }
  521.         else
  522.         {
  523.             /*
  524.              * No drawing engine attached to this window.
  525.              */
  526.             
  527.             gDrawContext [i] = NULL;
  528.         }
  529.     }
  530.     return (true);
  531. }
  532.  
  533. void TestDrawContextDelete (void)
  534. {
  535.     long        i;
  536.     
  537.     for (i = 0; i < kRAVE_Windows; ++i)
  538.     {
  539.         if (gDrawContext [i])
  540.         {
  541.             QADrawContextDelete (gDrawContext [i]);
  542.             gDrawContext [i] = NULL;
  543.         }
  544.     }
  545. }
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.